package com.cn.jx.manager.service.mtn.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.OrgRepository;
import com.cn.jx.manager.form.OrgForm;
import com.cn.jx.manager.po.Org;
import com.cn.jx.manager.service.mtn.IOrgServiceMtn;
import com.jx.enumType.OperatEnum;
import com.jx.util.ArrayUtil;
import com.jx.util.CodeUtil;
import com.jx.util.ObjectTransUtil;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("orgServiceMtn") 
@Transactional
public class OrgServiceImplMtn extends BaseService implements IOrgServiceMtn {
	
	@Autowired
	private OrgRepository orgDao;
	
	/* 删除组织 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void delOrg(List<String> ids, String opter) throws Exception {
		for (String id : ids) {
			List<Org> list = orgDao.findAllOrgByParent(id);
			if (ArrayUtil.listNotEmpty(list)) {
				// 说明有子项目 不能删除
				throw new Exception("不能删除，请先清理子部门再删除");
			}
		}
		// 删除组织树缓存
//		redisUtil.hdelItem("orgServiceQry", "findOrgTree");
		deleteEntityByIds(ids, Org.class);
		logUtil.LogMsg(Org.class, Utility.getListToStr(ids), OperatEnum.DELETE, opter);
	}

	/* 移除 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeOrg(List<String> ids, String opter) throws Exception {
		removeEntityByIds(ids, Org.class);
		logUtil.LogMsg(Org.class, Utility.getListToStr(ids), OperatEnum.REMOVE, opter);
	}
	
	/* 恢复 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key="orgServiceQry",value="'findOrgTree'")
	public void recoverOrg(List<String> ids, String opter) throws Exception {
		// 删除组织树缓存
		recoverEntityById(ids, Org.class);
		logUtil.LogMsg(Org.class, Utility.getListToStr(ids), OperatEnum.RECOVER, opter);
	}
	
	/* 保存更新*/
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
//	@RedisCacheRemove(key="orgServiceQry",value="'findOrgTree'")
	public void saveOrUpdateOrg(OrgForm item) throws Exception {
		if(Utility.isNotBlank(item.getId())){
			Org org=findEntityByPkid(item.getId(),Org.class);//得到库中的数据
			Org uo=ObjectTransUtil.getUpdateObj(org, item);
			if(uo!=null){
				uo.setOperatTime(new Date());
				uo.setOperatUser(item.getOperatUser());
				Org res=orgDao.save(uo);
//				redisUtil.hdelItem("orgServiceQry", "getOrgById="+res.getId());
				// 保存日志
				logUtil.LogMsg(Org.class, res.getId(), OperatEnum.UPDATE, item.getOperatUser());
			}
		}else{
			String maxcode=orgDao.findMaxOrgCode();
			String newcode=CodeUtil.getOrgCode(maxcode);
			Org org=Org.getInstance(item.getOperatUser());
			org.setOrgCode(newcode);
			org.setOrgName(item.getOrgName());
			org.setEnglishName(item.getEnglishName());
			org.setAddress(item.getAddress());
			org.setPhone(item.getPhone());
			org.setFax(item.getFax());
			org.setParentId(item.getParentId());
			Org orgs=	orgDao.save(org);
			// 保存日志
			logUtil.LogMsg(Org.class, orgs.getId(), OperatEnum.SAVE, item.getOperatUser());
		}
	}

	/* 清空 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeOrgAll(String opertor) throws Exception {
		removeAllEntity(Org.class);
		// 保存日志
		logUtil.LogMsg(Org.class, "ALL", OperatEnum.REMOVE, opertor);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key="orgServiceQry",value="'findLeaderIdByOrg='.concat(#orgId)")
	public void updateOrgLeader(String orgId, String userId,String opertor) throws Exception {
		orgDao.updateOrgLeader(userId, orgId);
		// 保存日志
		logUtil.LogMsg(Org.class, orgId, OperatEnum.ORG_SET_LEADER, opertor);
	}
}
